package com.example.webservice2.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.webservice2.controller.model.SysUser;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

/**
 * @author jie
 * @date 2021/4/25 -9:35
 */
@RestController
public class TestController {

    @GetMapping("/hello")
    public Object hello() throws Exception{
        try{
            JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            URL url= new URL("http://localhost:8081/wbs/ws/api?wsdl");
            Client client = dcf.createClient(url);
            Object[] objects = client.invoke("getList");
            return objects[0];

        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        String jsonStr= "[{\"id\":\"1\",\"name\":\"Jie1\"},{\"id\":\"2\",\"name\":\"Jie2\"}]";
        JSONArray array = JSONArray.parseArray(jsonStr);
        List<SysUser> sysUserList = JSONObject.parseArray(array.toJSONString(), SysUser.class);
        System.out.println(sysUserList);

    }

    public  String get() throws Exception{
        URL url=new URL("http://10.0.0.132:8081/hello");
        HttpURLConnection conn=(HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        //结果码=200
        if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
            InputStream is=conn.getInputStream();
            //内存流 ，
            ByteArrayOutputStream boas=new ByteArrayOutputStream();
            byte[] buffer=new byte[1024];
            int len=-1;
            while((len=is.read(buffer))!=-1){
                boas.write(buffer, 0, len);
            }
            System.out.println("GET请求获取的数据:"+boas.toString());
            boas.close();
            is.close();
            return boas.toString();
        }
        return null;
    }
}
